package com.athuiju.fenxiaoback.service.impl;

import com.athuiju.fenxiaoback.entity.TbSpu;
import com.athuiju.fenxiaoback.entity.TbSpuDetail;
import com.athuiju.fenxiaoback.entity.vo.SpuInfoVo;
import com.athuiju.fenxiaoback.mapper.TbSpuMapper;
import com.athuiju.fenxiaoback.service.TbSkuService;
import com.athuiju.fenxiaoback.service.TbSpuDetailService;
import com.athuiju.fenxiaoback.service.TbSpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 服务实现类
 * </p>
 *
 * @author athuiju
 * @since 2021-06-30
 */
@Service
public class TbSpuServiceImpl extends ServiceImpl<TbSpuMapper, TbSpu> implements TbSpuService {
    @Autowired
    private TbSkuService skuInfoService;

    @Autowired
    private TbSpuDetailService spuDetailService;

    @Override
    public SpuInfoVo getSkuInfoBySkuId(Long skuId) {
        SpuInfoVo spuInfoVo= new SpuInfoVo();
        TbSpu spu = this.getById(skuInfoService.getById(skuId).getSpuId());
        BeanUtils.copyProperties(spu,spuInfoVo);
        spuInfoVo.setBrandId(spu.getBrandId());
        spuInfoVo.setCatalogId(spu.getCid3());
        spuInfoVo.setId(Long.parseLong(spu.getId()));
        spuInfoVo.setCreateTime(spu.getGmtCreate());
        spuInfoVo.setPublishStatus(spu.getSaleable() ? 1:0);
        TbSpuDetail spuDetail = spuDetailService.getOne(new QueryWrapper<TbSpuDetail>().eq("spu_id", spu.getId()));
        spuInfoVo.setSpuDescription(spuDetail.getDescription());
        spuInfoVo.setSpuName(spu.getTitle());
        spuInfoVo.setUpdateTime(spu.getGmtModified());
        spuInfoVo.setWeight(new BigDecimal(1));
        return spuInfoVo;
    }
}
